package aop.log.advice;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.logging.Logger;

public class LogProxy implements InvocationHandler{
	public Object proxyOb;
	public LogProxy(Object obj) {
		this.proxyOb=obj;
	}
	//Create a proxy object and bind the proxy object
	public static Object bind(Object obj) {
		Class<?> cl =obj.getClass();
		return Proxy.newProxyInstance(cl.getClassLoader(), cl.getInterfaces(),new LogProxy(obj));
		
	}
	
	
	public Object invoke(Object Proxy, Method method, Object args[]) throws Throwable {
		beforeLogin();
		
		Object object = method.invoke(proxyOb, args);
		
		afterLogin();
		return object;
	}

	private void logging(String msg) {
		Logger logger=Logger.getLogger(LogProxy.class.getName());
		logger.info(msg);
	}
	
	private void afterLogin() {	
		logging("Log inforemation after the user login operation has been performed");
	}

	private void beforeLogin() {
		logging("Log information before the user login operation is executed");	
	}

}
